package com.sz.common.pay.citic.core;

import com.sz.common.pay.common.SequenceGenerator;
import com.sz.common.pay.citic.xml.XmlMapper;
import com.sz.common.pay.common.PayLogger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by siqishangshu on 17/10/16.
 *
 *  CITIC  only use post for data exchange
 */
public class CITICConnector {

    private static PayLogger logger = PayLogger.getLogger(CITICConnector.class);

    /**
     * object post response object (entity)
     * @param request
     * @param responseClazz
     * @return object
     */
    public static Object post(Object request,Class responseClazz) {
        return post(ConnectParam.DEFAULT_URL,request,responseClazz);
    }

    /**
     * use object to xml  (entity)
     * @param url
     * @param request
     * @param responseClazz
     * @return object
     */
    public static Object post(String url, Object request,Class responseClazz) {
        return XmlMapper.convertToObject(responseClazz,post(url, XmlMapper.convertToXml(request)));
    }

    /**
     * use param String to post
     * @param param
     * @return string
     */
    public static String post(String param) {
       return post(ConnectParam.DEFAULT_URL,param);
    }

    /**
     *  URL  POST
     *
     * @param url
     *
     * @param param
     *
     * @return string
     */
    public static String post(String url, String param) {
        String sequence =  SequenceGenerator.getSequenceNumber();
        logger.info(sequence, param);
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestProperty("accept", "text/xml; charset=CHARSET_GBK");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36");
            conn.setRequestMethod(ConnectParam.METHOD);
            conn.setConnectTimeout(ConnectParam.CONNECT_TIME_OUT);
            conn.setReadTimeout(ConnectParam.READ_TIME_OUT);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            if( ConnectParam.SUCCEED == conn.getResponseCode()){
                logger.info(sequence,"SUCCEED");
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            }else{
                logger.error(sequence,"post not succeed Response Code : "+conn.getResponseCode());
            }
        } catch (Exception e) {
            logger.error(sequence,e);
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                logger.error(sequence,ex);
            }
        }
        logger.info(sequence,result);
        return result;
    }

}
